package com.drone.drone_server.controller;

import com.drone.drone_server.api.ApiResponse;
import com.drone.drone_server.dao.DroneRouteDao;
import com.drone.drone_server.entity.DroneAction;
import com.drone.drone_server.entity.DroneRoute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/routes")
public class RouteController {

    @Autowired
    private DroneRouteDao droneRouteDao;

    // 获取所有路线
    @GetMapping("/getAllRoutes")
    public ApiResponse getAllRoutes() {
        List<DroneRoute> routes = droneRouteDao.selectList(null);
        return ApiResponse.success(routes);
    }

    // 获取所有仍在使用的路线
    @GetMapping("/getAllActiveRoutes")
    public ApiResponse getAllActiveRoutes() {
        List<DroneRoute> routes = droneRouteDao.selectAllActiveRoutes();
        return ApiResponse.success(routes);
    }

    // 启用/弃用路线
    @PutMapping("/toggleActiveStatus")
    public ApiResponse toggleActiveStatus(@RequestParam Integer routeId,
                                          @RequestParam Boolean isActive) {
        DroneRoute route = droneRouteDao.selectById(routeId);
        if (route == null) {
            return ApiResponse.fail("路线不存在");
        }
        route.setIsActive(isActive);
        int result = droneRouteDao.updateById(route);
        return result > 0 ? ApiResponse.success("状态更新成功") : ApiResponse.fail("状态更新失败");
    }


    // 新增路线
    @PostMapping("/createRoute")
    public ApiResponse createRoute(@RequestBody DroneRoute route) {
        route.setRouteId(null);
        int result = droneRouteDao.insert(route);
        return result > 0 ? ApiResponse.success("路线创建成功") : ApiResponse.fail("路线创建失败");
    }
}
